<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------
/**
 *  Entrance File of the Control class.
 *
 * @Author:    zaki<>
 * @Package:   Interface of the View class. 
 * @Version:   $Id: zy_nvestment.class.php 2011-07-06 21:26:23 $
 * @Show Note: 
 */
Class zy_nvestment extends TheAdmin
{
		/* DataBase object */
		public $db;
	
		/* View the data page limit items */
		public $pageSize = PAGENO;
		
		/**
		 *	Construct this module class.
		*/
		public function zy_nvestment()
		{
		}
		public function set()
		{
			$this->TheAdmin();
			//定义模版
			$this->tplFile ="zy_nvestment.tpl";
			$this->admin_user_id = $this->userRequest->get('admin_user_id');
			//取得数据库连接
			$this->db = Factory::getDB("zy_nvestment");
			//取得用户对于该类的权限
			$user_p = $this->get_user_popedom_by_class_name(__CLASS__);
			$class_p_arr = $this->show_p();
			//取得该类的权值数组
			$class_method = $class_p_arr['class_method'];
			//取得用户操作菜单
			$menu = array();
			foreach ($class_method as $k=>$v)
			{
				if( (parse_power($user_p,$v[2]) && (isset($v[3]) ?$v[3] :false)) || IS_ROOT)
				{
					array_push($menu,array('url'=>"?c=".__CLASS__."&m={$v[0]}",'title'=>$v[1]));
				}
			}
			//用户有权菜单生成 可以添加其他操作
			$this->smarty->assign('menus',$menu);
			//$this->smarty->assign('admin_info',get_admin_info());//取得管理员用户数组
			$this->smarty->assign('sell_type_arr',get_sell_type_arr());//购置性质数组
			$this->smarty->assign('project_arr',get_project_arr());//项目数组
			$this->smarty->assign("main_menus",$this->get_user_menus());
		}
	
		/**
		 *	Call class construct method.
		*/
		public function __construct()
		{
				$this->zy_nvestment();
		}
		public function _display()
		{
			$this->smarty->display($this->tplFile);
		}
		/**
		 *	default method.
		*/
		public function show()
		{
				//主要是正常投资计划页面展示 或者 添加批复是跳转页展示
				$is_jump = isset($_GET['is_jump']) ? intval($_GET['is_jump']) : 0;
				$is_show_sell = isset($_GET['is_show_sell']) ? intval($_GET['is_show_sell']) : 0;
				
				$col = "add_time";
				$where = 1;
				//投资编号检索
				$investment_no = isset($_GET['investment_no']) ? trim($_GET['investment_no']) : '';
				if (!empty($investment_no)) {
					$where .= " AND investment_no like '%{$investment_no}%' ";
				}
				//申请人检索
				$admin_user_id = isset($_GET['admin_user_id']) ? trim($_GET['admin_user_id']) : '';
				if (!empty($admin_user_id)) {
					$where .= " AND admin_user_id = {$admin_user_id} ";
				}
				//申请开始时间
				$application_date_start = isset($_GET['application_date_start']) && !empty($_GET['application_date_start']) ? strtotime(($_GET['application_date_start'])) : '';
				//申请结束时间
				$application_date_end = isset($_GET['application_date_end']) && !empty($_GET['application_date_end']) ? strtotime($_GET['application_date_end']) : '';
				
				if (!empty($application_date_start)) {
					$where .= " AND application_date >= {$application_date_start} ";
				}
				
				if (!empty($application_date_end)) {
					$application_date_end = $application_date_end + 86400 - 1 ;//多加一天
					$where .= " AND application_date <= {$application_date_end} ";
				}
				
				//取出所有计划 去掉没有设备的计划 和 已经批复过的
				if ($is_jump) {
					$investment_no_arr = array();
					$arr = $this->db->getAll($where,$col);
					foreach ($arr as $key=>$value)
					{
						//判断计划中是否有设备
						$array = $this->get_mc_by_investment_no($value['investment_no']);
						//print_r($array);
						if (empty($array)) {
							unset($arr[$key]);
						}else{
							$investment_no_arr[] = "'".$value["investment_no"]."'"; 
						}
					}
					if ($investment_no_arr) {
						$investment_no_str = implode(',',$investment_no_arr);
						$where .= " AND investment_no IN ({$investment_no_str}) ";
					}else{
						alertMsg('-5014');	
					}					
				}
				$rowNo = $this->db->getSumNumber($this->db->tableName,$where);
				//数据库执行sql语句出错
				//取得错误的sql语句
				/*$error_sql = end($this->db->getDebugMsg());
				echo $error_sql;exit;*/
				if( 0 > $rowNo) goToIndex($rowNo);
				//分页
				$page = Factory::getBaseClass('pagination');
				$page->set($rowNo,$this->pageSize);
				$numrows = $this->pageSize;
				$offset  = $page->getSqlMove();
				$arr = $this->db->getAll($where,$col,$numrows,$offset);
				admin_log($this->admin_user_id,__METHOD__,'查看',1);
				//数据库执行select语句出错
				//取得错误的sql语句
				//$error_sql = end($this->db->getDebugMsg());
				//echo $error_sql;
				if(0 > $arr ) goToIndex($arr);
				
				$this->smarty->assign('arr',$arr);
				//默认方法
				$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
				if (empty($is_jump)) {
					$this->smarty->assign('act','list');
					
				}else{
					$this->smarty->assign('is_show_sell',$is_show_sell);
					$this->smarty->assign('act','show_add');
				}
				$this->smarty->display($this->tplFile);
		}
		
		/**
		 *	add method.
		*/
		public function add()
		{
				//添加数据
				if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
				{
						//使用模版，展示提交界面
						$this->smarty->assign('act_title','添加');
						$this->smarty->assign('act','add');
						$this->smarty->display($this->tplFile);
				} else {
						//用户提交，把数据插入数据库
						$temp = $this->getValue();
						$insrt = $this->db->insert_zy_nvestment($temp);
						$error_no = 0 > $insrt ?  $insrt :'-1501';
						admin_log($this->admin_user_id,__METHOD__,"id={$insrt}",2);
						//取得错误的sql语句
						//$error_sql = end($this->db->getDebugMsg());
						//echo $error_sql;
						if ($insrt < 0) {
							alertMsg($error_no, 'index.php?c=zy_nvestment');
						}else{
							redirectPage('index.php?c=zy_nvestment');
						}
				}
		}
		
		/**
		 *	edit method.
		*/
		public function edit()
		{
				//取得id，也就是唯一索引
				$investment_no = isset($_REQUEST['investment_no']) ? trim($_REQUEST['investment_no']) : 0 ;
				//取得是否查看详情
				$is_show = isset($_REQUEST['is_show']) ? intval(trim($_REQUEST['is_show'])) : 0 ;
				if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
				{
						//使用模版，展示提交界面
						$result = $this->db->getOne($investment_no);
						if(empty($result))
						{
							alertMsg('-2001', 'index.php?c=zy_nvestment');
						}
						foreach ($result AS $k=>$v)
						{
							$this->smarty->assign("{$k}",$v);
						}
						$this->smarty->assign('is_show',$is_show);
						$this->smarty->assign('act_title','修改');
						$this->smarty->assign('act','edit');
						$this->smarty->display($this->tplFile);
				} else {
						//用户提交，把数据插入数据库
						//设置where条件
						$where = "investment_no = '$investment_no'";
						$temp = $this->getValue(1);
						$update = $this->db->update_zy_nvestment($temp,$where);
						admin_log($this->admin_user_id,__METHOD__,$where,2);
						$error_no = 0 > $update ?  $update :'-1502';
						//取得错误的sql语句
						//$error_sql = end($this->db->getDebugMsg());
						//echo $error_sql;
						if ($update < 0) {
							alertMsg($error_no, 'index.php?c=zy_nvestment');
						}else{
							redirectPage('index.php?c=zy_nvestment');
						}
				}
		}
		/**
		 *	browse method.
		*/
		public function browse() {
			$investment_no = isset($_REQUEST['investment_no']) ? trim($_REQUEST['investment_no']) : 0 ;
			//使用模版，展示提交界面
			$result = $this->db->getOne($investment_no);
			if(empty($result))
			{
				alertMsg('-2001', 'index.php?c=zy_nvestment');
			}
			foreach ($result AS $k=>$v)
			{
				$this->smarty->assign("{$k}",$v);
			}
			$where = "investment_no = '{$investment_no}' ";
			if(isset($_GET['cn_name'])){
				$where.=" and mc_cn_name like '%{$_GET['cn_name']}%'";
			}
			$col = 'v_mc_id';
			//设备展示页面
			$rowNo = $this->db->getSumNumber('zy_mc_type',$where);
			
			if( 0 > $rowNo) goToIndex($rowNo);
			//分页
			$page = Factory::getBaseClass('pagination');
			$page->set($rowNo,$this->pageSize);
			$numrows = $this->pageSize;
			$offset  = $page->getSqlMove();
			$zy_mc_type_db = Factory::getDB('zy_mc_type');
			$zy_mc_type_db->setOrderBy();
			$zy_mc_type_arr = $zy_mc_type_db->getAll($where,$col,$numrows,$offset);
			//print_r($zy_mc_type_arr);
			admin_log($this->admin_user_id,__METHOD__,'查看',1);
			//数据库执行select语句出错
			//取得错误的sql语句
			//$error_sql = end($this->db->getDebugMsg());
			//echo $error_sql;
			//判断设备是否已经批复过 如果已经批复过则不在显示删除和修改
			if ($zy_mc_type_arr) {
				$zy_plan_reply_mc_db = Factory::getDB('zy_plan_reply_mc');
				foreach ($zy_mc_type_arr as $key=>$value)
				{
					$zy_mc_type_arr[$key]['is_exists'] = $zy_plan_reply_mc_db->check_is_exist_by_mc_id($value['v_mc_id']);//验证是否存在批复 如果1为已经存在批复  0为未存在批复
				}
			}
			//print_r($zy_mc_type_arr);
			$is_reply = $this->check_is_reply_exist($investment_no);//检测是否有批复记录  如果已经批复则不能在添加设备
			$this->smarty->assign('is_reply',$is_reply);
			$this->smarty->assign('zy_mc_type_arr',$zy_mc_type_arr);//虚拟设备信息
			//默认方法
			$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
			$this->smarty->assign('act_title','查看');
			$this->smarty->assign('act','browse');
			$this->smarty->display($this->tplFile);
		}
		/**
		 *	del method.
		*/
		public function del()
		{
				//取得id，也就是唯一索引
				$investment_no = isset($_REQUEST['investment_no']) ? trim($_REQUEST['investment_no']) : 0 ;
				//判断是否存在虚拟设备 如果存在则不能删除
				$temp = $this->get_mc_type_by_no($investment_no);
				if (!empty($temp)) {
					alertMsg('-5002');
				}
				$where = "investment_no = '{$investment_no}' ";
				$del=$this->db->delete_zy_nvestment($where);
				admin_log($this->admin_user_id,__METHOD__,$where,3);
				$error_no = 0 > $del ?  $del :'-1503';
				//取得错误的sql语句
				//$error_sql = end($this->db->getDebugMsg());
				//echo $error_sql;
				if ($del < 0) {
					alertMsg($error_no, 'index.php?c=zy_nvestment');
				}else{
					redirectPage('index.php?c=zy_nvestment');
				}
		}
		/**
		 *	get request method.
		*/
		public function &getValue($isEdit=0)
		{
			$temp = array();
			$check = Factory::getBaseClass("checkBase");
			$temp['investment_no'] = isset($_POST['investment_no']) ? htmlspecialchars(trim($_POST['investment_no'])) : '';//投资计划自编号(添加后不让修改)
			if (empty($temp['investment_no'])) {
				alertMsg('-2055');
			}
			if($isEdit){
				//$temp['investment_no'] = $_POST['investment_no'];
			} else {
				$temp['add_time'] = time();//添加时间
				//查看添加是否重复
				if ($this->check_is_exist($temp['investment_no'])) {
					alertMsg('-2062');
				}
			}
			//补充其他的字段
			
			$temp['application_date'] = isset($_POST['application_date']) && !empty($_POST['application_date']) ? strtotime($_POST['application_date']) : 0;//申请时间
			$temp['plan_notes'] = isset($_POST['plan_notes']) ? htmlspecialchars(trim($_POST['plan_notes'])) : '';//计划备注
			$temp['Investment_state'] = isset($_POST['Investment_state']) ? intval($_POST['Investment_state']) : 1;//状态
			$temp['admin_user_id'] = isset($_POST['admin_user_id']) ? intval($_POST['admin_user_id']) : 0;//申请人
			if (empty($_POST['admin_user_id'])) {
				alertMsg('-2056');
			}
			admin_log($this->admin_user_id,__METHOD__,serialize($temp),2);	
			return $temp;
		}
		/**
		 *	get class method and popedom.
		*/
		public function show_p()
		{
			//该方法为，提供用户使用方法定义，非web用户权限相关不用写
			return array(
				/* 定义该类中文名称*/
				'class_name'=>array(__CLASS__,'投资计划编号表管理'),
				'class_method' =>array(
					//array（方法名，中文解释，权值,是否为菜单）
					'show'  => array('show','列表',1,true),
					'add'  => array('add','添加',2,true),
					'edit'  => array('edit','编辑',4,false),
					'del'  => array('del','删除',8,false),
					'browse' => array('browse', '查看', 16, false),
					'show_p'=> array('show_p','',0,false)
				)
			);
		}
		/**
		 * 根据计划自编号取得虚拟设备信息
		 *
		 * @param unknown_type $investment_no
		 * @return unknown
		 */
		function get_mc_type_by_no($investment_no)
		{
			$zy_mc_type_db = Factory::getDB('zy_mc_type');
			$temp = $zy_mc_type_db->get_mc_by_investment_no($investment_no);
			return $temp;
		}
		/**
		 * 检测计划编号是否已经存在
		 *
		 * @param unknown_type $investment_no
		 * @return unknown
		 */
		function check_is_exist($investment_no)
		{
			$zy_nvestment_db  = Factory::getDB('zy_nvestment');
			$temp = $zy_nvestment_db->getOne($investment_no);
			return is_array($temp) && !empty($temp) ? 1 : 0;
		}
		/**
		 * 检测该计划是否被批复
		 *
		 * @param unknown_type $investment_no
		 * @return unknown
		 */
		function check_is_reply_exist($investment_no)
		{
			$zy_plan_reply_db = Factory::getDB('zy_plan_reply');
			return $zy_plan_reply_db->check_is_reply_by_investment_no($investment_no);
		}
		/**
		 * 根据申请编号取得该下面的虚拟设备信息
		 *
		 * @param unknown_type $investment_no
		 * @return unknown
		 */
		function get_mc_by_investment_no($investment_no)
		{
			$zy_mc_type_db = Factory::getDB('zy_mc_type');
			$zy_plan_reply_mc_db = Factory::getDB('zy_plan_reply_mc');
			$arr = $zy_mc_type_db->get_mc_by_investment_no($investment_no);
			if ($arr) {
				foreach ($arr as $key=>$value)
				{
					//取得每个虚拟设备ID被批复的数字 如果批复数量大于等于申请数量 则注销掉不显示
					$plan_reply_num = $zy_plan_reply_mc_db->plan_reply_num_total($value['v_mc_id']);
					if ($value['plan_no'] <= $plan_reply_num) {
						unset($arr[$key]);
					}else{
						$arr[$key]['last_no'] = $value['plan_no']-$plan_reply_num;
					}
				}
			}
			return $arr;
		}
}

?>